<template>
	<view class="index-content">
		<!-- 		<TopBar :indexflag="true" :combakflag="false" :lang="true" :bottom="true" title="DikiDiki"
			barstyle="background-color:rgba(0,0,0,0)"></TopBar> -->
		<HeaderTitle title="DikiDiki" :bottom="true" :lang="true" :mobile="true"></HeaderTitle>
		<Header></Header>
		<view class="topnav">
			<view :class="type==item.type?'item acitem':'item'" v-for="(item,index) in navList"
				@click="changeNav(item.type)">
				{{$t(item.name)}}
			</view>
		</view>
		<view class="order-list">
			<scroll-view scroll-y="true" style="height:100%;width:100%" @scrolltolower="scrolldata">
				<!-- <u-empty v-if="orderList.length==0" style="padding-top: 200rpx;" mode="order" iconSize="100"
					textSize="14" :text="$t('OrderEmpty')">
				</u-empty> -->
				<view class="" v-for="(item,index) in orderList" v-if="type=='new' && orderList.length">
					<view class="list" :dir="i18nLocale=='ug'?'rtl':''" v-for="(sub_item,sub_index) in item.orders"
						@click="handdetail(sub_item.id)">
						<view class="time">
							<text>{{$t('TimePlaced')}}:</text>
							<text dir="ltr">{{sub_item.paid_at}}</text>
							<u-icon name="shopping-cart-fill" color="#45bc1b" size="25" v-if="type=='new'"></u-icon>
							<u-icon name="bag-fill" color="#45bc1b" size="25" v-else-if="type=='stay_take'"></u-icon>
							<u-icon name="car-fill" color="#45bc1b" size="25" v-else-if="type=='in_send'"></u-icon>
							<u-icon name="checkmark-circle-fill" color="#45bc1b" size="25" v-else></u-icon>
						</view>
						<view class="address">
							<view class="steps">
								<!-- <view>{{$t('Take')}}</view><text></text> -->
								<view>{{$t('Give')}}</view>
							</view>
							<view class="">
								{{sub_item.address.detail}}
							</view>
						</view>
						<view class="sub_title"> <text
								style="width:10rpx;height:50rpx;border-radius:20rpx;background-color: #45bc1b;"></text>
							{{sub_item.out_trade_no}}
						</view>
						<view class="money">
							<!-- <view><text>{{$t('DeliveryFees')}}：</text>¥{{sub_item.fee}}</view> -->
							<view><text>{{$t('TotalPrice')}}：</text>¥{{sub_item.fee}}</view>
						</view>
						<view class="remarks">
							{{$t('Remark')}}：<text>{{sub_item.remark}}</text>
						</view>
						<view class="remarks">
							{{$t('CustomerTime')}}：<view dir="ltr">{{sub_item.arrived_at}}</view>
						</view>
						<view class="btn" @click.stop="getGraborder(sub_item.id)" v-if="type=='new'">
							{{$t('TakeOrders')}}
						</view>
					</view>
				</view>
				<!-- 我的订单 -->
				<u-empty v-if="myorderList.length==0 && type!='new'" style="padding-top: 200rpx;" mode="order"
					iconSize="100" textSize="14" :text="$t('OrderEmpty')">
				</u-empty>
				<view class="" v-if="type!='new'">
					<view class="list" :dir="i18nLocale=='ug'?'rtl':''" v-for="(item,index) in myorderList"
						@click="handdetail(item.id)">
						<view class="time">
							<text>{{$t('TimePlaced')}}:</text>
							<text dir="ltr">{{item.paid_at}}</text>
							<u-icon name="shopping-cart-fill" color="#45bc1b" size="25" v-if="type=='new'"></u-icon>
							<u-icon name="bag-fill" color="#45bc1b" size="25" v-else-if="type=='stay_take'"></u-icon>
							<u-icon name="car-fill" color="#45bc1b" size="25" v-else-if="type=='in_send'"></u-icon>
							<u-icon name="checkmark-circle-fill" color="#45bc1b" size="25" v-else></u-icon>
						</view>
						<view class="address">
							<view class="steps">
								<!-- <view>{{$t('Take')}}</view><text></text> -->
								<view>{{$t('Give')}}</view>
							</view>
							<view class="">
								{{item.address.detail}}
							</view>

						</view>
						<view class="sub_title"> <text
								style="width:10rpx;height:50rpx;border-radius:20rpx;background-color: #45bc1b;"></text>
							{{item.out_trade_no}}
						</view>
						<view class="money">
							<!-- <view><text>{{$t('DeliveryFees')}}：</text>¥{{item.fee}}</view> -->
							<view><text>{{$t('TotalPrice')}}：</text>¥{{item.fee}}</view>
						</view>
						<view class="remarks">
							{{$t('Remark')}}：<text>{{item.remark}}</text>
						</view>
						<view class="remarks">
							{{$t('CustomerTime')}}：<view dir="ltr">{{item.arrived_at}}</view>
						</view>
						<view class="btns" v-if="type=='stay_take'">
							<!-- <view @click.stop="handphone(item.start_infor.start_mobile)">{{$t('ContactMerchant')}}
							</view> -->
							<view class="btn1" @click.stop="handshop(item.id)">{{$t('ConfirmArrival')}}</view>
						</view>
						<view class="btns" v-if="type=='in_send'">
							<view @click.stop="handphone(item.address.mobile)">{{$t('CallBuyer')}}</view>
							<view class="btn1" @click.stop="handservice(item.id)">{{$t('ConfirmDelivery')}}</view>
						</view>
						<view class="btn" style="background-color:#999999;" v-if="type=='end'">
							{{$t('Finish')}}
						</view>
					</view>
				</view>
				<Loading :show="tabloadshow" :text="$t('Loading')" style="width:100%;height:100%;position: absolute;">
				</Loading>
			</scroll-view>
		</view>
		<u-toast ref="uToast" :message="notmessage"></u-toast>
		<Loading :show="loadshow" bgColor="" iconName="circle" :text="$t('Loading')"></Loading>
	</view>
</template>

<script>
	import TopBar from "@/components/TopBar.vue"
	import Header from "./Header.vue"
	import HeaderTitle from "@/components/HeaderTitle.vue"
	import Navgtion from "./h5navgtion.vue"
	import {
		getorder,
		getmyorder,
		getgraborder,
		putorderstatus
	} from '@/api/all.js';

	export default {
		components: {
			TopBar,
			Header,
			HeaderTitle,
			Navgtion
		},
		mounted() {
			if (this.$store.state.riderinfo) {
				this.getOrder();
			}
		},
		data() {
			return {
				acnav: 0,
				navList: [{
						type: 'new',
						name: 'NewTasks'
					},
					{
						type: 'stay_take',
						name: 'PickDelivery'
					},
					{
						type: 'in_send',
						name: 'Shipping'
					},
					{
						type: 'end',
						name: 'Finish'
					}
				],
				type: 'new',
				orderList: [],
				myorderList: [],
				notmessage: '请仔细填写!',
				pageSize: 5,
				page: 1,
				total: 0,
				to: 1,
				tabloadshow: false,
				loadshow: false,
			}
		},
		computed: {
			i18nLocale() {
				return this.$i18n.locale;
			},
		},
		methods: {
			changeNav(type) {
				this.type = type;
				this.page = 1;
				this.total = 0;
				this.to = 1;
				this.tabloadshow = true;
				if (type == 'new') {
					this.getOrder();
				} else if (type == 'stay_take') {
					this.myorderList = [];
					this.getmyOrder(1)

				} else if (type == 'in_send') {
					this.myorderList = [];
					this.getmyOrder(2)
				} else {
					this.myorderList = [];
					this.scrolldata()

				}
			},
			scrolldata() {
				if (this.to != this.total) {
					this.loadshow = true;
					this.getmyOrder(4)
				} else {

				}
			},
			handdetail(id) {
				uni.navigateTo({
					url: '/pages/index/orderdatail?id=' + id
				})

			},
			getOrder() {
				let that = this;
				getorder({}).then((res) => {
					that.tabloadshow = false;
					console.log(res, '++++++')
					if (res.code == 200) {
						let data = res.body.data;
						that.orderList = data;
					}
				})
			},
			getmyOrder(status) {
				let that = this;
				getmyorder({
					status: status,
					per_page: that.pageSize,
					page: that.page,
				}).then((res) => {
					that.tabloadshow = false;
					console.log(res, '++++++')
					if (res.code == 200) {
						let data = res.body;
						if (status == 4) {
							that.total = data.total;
							that.to = data.to;
							that.page++;
							let mydata = that.myorderList.concat(data.data);
							that.myorderList = mydata;
							that.tabloadshow = false;
							that.loadshow = false;
						} else {
							that.myorderList = data.data;
						}

					}
				})
			},
			//抢单子
			getGraborder(id) {
				let that = this;
				getgraborder({
					order_id: id
				}).then((res) => {
					console.log(res, '++++++')
					if (res.code == 200) {
						let data = res.body.data;
						that.orderList = data;
					} else {
						that.$refs.uToast.show({
							top: 0,
							message: that.$t('OrderTaken'),
							color: '#fff',
							bgColor: '#f0ba53'
						});
						that.getOrder();
					}
				})

			},
			//骑手已到店
			handshop(id) {
				let that = this;
				putorderstatus({
					id: id,
					status: 2
				}).then((res) => {
					if (res.code == 200) {
						// 422//别人抢了
						console.log(res, '++++++')
						// that.getmyOrder(2)
						// that.type = 'in_send';
						
					}
				})
			},
			//订单已配送
			handservice(id) {
				let that = this;
				putorderstatus({
					id: id,
					status: 4
				}).then((res) => {
					if (res.code == 200) {
						console.log(res, '++++++')
						// that.getmyOrder(2)
						// that.type = 'end';
					}
				})
			},
			handphone(mobile) {
				uni.makePhoneCall({
					phoneNumber: String(mobile)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.index-content {
		width: 100%;
		height: 100%;
		box-sizing: border-box;

		.topnav {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-around;
			background-color: $uni-bg-color-box;
			box-sizing: border-box;
			padding: 20rpx 20rpx;
			font-size: $uni-font-size-base;

			.acitem {
				color: $uni-color-topic;
			}
		}

		.order-list {
			width: 100%;
			height: calc(100% - 305rpx);
			box-sizing: border-box;
			padding: 0rpx 20rpx;

			.list {
				width: 100%;
				background: #FFFFFF;
				box-shadow: $uni-bg-shadow;
				border-radius: $uni-border-radius-lg;
				margin-top: 20rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 0 40rpx;
				box-sizing: border-box;

				.time {
					width: 100%;
					height: 80rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					font-size: 28rpx;
					color: #000;

					text {
						color: #808080;
					}

					image {
						width: 80rpx;
						height: 80rpx;
					}
				}

				.address {
					width: 100%;
					background: #FFFFFF;
					// border: 1rpx solid #E6E6E6;
					border-radius: 20rpx;
					display: flex;
					align-items: flex-start;
					flex-direction: row;
					justify-content: space-between;
					padding: 10rpx 20rpx;

					.steps {
						display: flex;
						flex-direction: column;
						align-items: center;
						justify-content: space-between;

						view {
							width: 60rpx;
							height: 60rpx;
							background: $uni-color-topic;
							border-radius: 50%;
							background-color: $uni-color-topic;
							font-size: 20rpx;
							color: #F0F3F8;
							display: flex;
							align-items: center;
							justify-content: center;
						}

						view:last-child {
							background: #F3444B;
						}

						text {
							display: block;
							width: 1rpx;
							height: 60rpx;
							border: 1rpx dashed #F0F3F8;
						}
					}

					.name {
						width: 80%;
						height: 195rpx;
						display: flex;
						flex-direction: column;
						justify-content: space-between;

						.view {
							display: flex;
							width: 100%;
							flex-direction: column;
							height: 80rpx;

							view {
								width: 100%;
								font-size: 28rpx;
								color: #000;
								overflow: hidden;
								white-space: nowrap;
								text-overflow: ellipsis;
							}

							text {
								width: 100%;
								font-size: 26rpx;
								color: #808080;
								margin-top: 10rpx;
								overflow: hidden;
								white-space: nowrap;
								text-overflow: ellipsis;
							}
						}
					}

					.distance {
						height: 150rpx;
						display: flex;
						flex-direction: column;
						align-items: flex-end;
						justify-content: space-between;
						font-size: 28rpx;
						color: #000;

						.item {
							display: flex;
							flex-direction: column;
							align-items: center;
							justify-content: center;
							margin-bottom: 15rpx;
						}
					}

				}

				.sub_title {
					width: 100%;
				}

				.money {
					width: 100%;
					margin-top: 20rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					view {
						font-size: 28rpx;
						color: #33AAFC;

						text {
							color: #000000;
						}
					}

					view:last-child {
						color: #E40E04;
					}
				}

				.remarks {
					width: 100%;
					margin-top: 20rpx;
					display: flex;
					align-items: center;
					font-size: 28rpx;
					color: #808080;

					text {
						color: #000000;
					}

					view {
						color: #33AAFC;
					}
				}

				.btn {
					width: 280rpx;
					height: 68rpx;
					background: $uni-color-topic;
					border-radius: 10rpx;
					margin: 20rpx 0;
					font-size: 28rpx;
					color: #fff;
					display: flex;
					align-items: center;
					justify-content: center;
				}

				.btns {
					width: 100%;
					padding: 20rpx 0;
					display: flex;
					align-items: center;
					justify-content: center;

					view {
						width: 100%;
						height: 68rpx;
						border: 1rpx solid $uni-color-topic;
						border-radius: 10rpx;
						margin: 0 10rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						color: $uni-color-topic;
						font-size: 28rpx;
					}

					.btn1 {
						color: #fff;
						background-color: $uni-color-topic;
					}
				}
			}
		}
	}
</style>